package com.key.controller.admin.assetManagement;

import com.alibaba.fastjson.JSONObject;
import com.key.common.R;
import com.key.common.dto.AssetChangeAdvDTO;
import com.key.common.dto.AssetChangeDTO;
import com.key.common.vo.AssetChangeUpdateVO;
import com.key.common.vo.AssetChangeVO;
import com.key.common.vo.ChangeAssetsListVO;
import com.key.entity.AssetChange;
import com.key.service.AssetChangeService;
import io.swagger.annotations.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.*;

/*
*
 * @description:实物信息变更Controller
 * @author: jianjiawei
 * @date: 2023/2/17 10:45
 **/
@Api(value = "实物信息变更API",tags = "实物信息变更API")
@Controller
@RequestMapping("/assetChange")
public class AssetChangeController {

    @Autowired
    AssetChangeService acService;

    /*
    *
     * @description:新建实物信息变更的表单数据回显
     * @author: jianjiawei
     * @date: 2023/2/17 10:57
     **/
    @ApiOperation(value = "新建实物表单数据回显接口",notes = "新建实物信息变更的表单数据回显",httpMethod = "get", response = R.class)
    @RequestMapping(value = "/showAssetsChange",method = RequestMethod.GET)
    @ResponseBody
    public R showAssetsChange(){

        return acService.showAssetsChange();
    }

/*
*
 * @description:表单数据保存接口
 * @author: jianjiawei
 * @date: 2023/2/17 18:07
 **/
    @ApiOperation(value = "表单数据保存接口",notes = "创建新的实物信息变更清单",httpMethod = "post", response = R.class)
    @ResponseBody
    @RequestMapping(value = "/saveAssetChange",method = RequestMethod.POST)
    public R saveAssetChange(@RequestBody @ApiParam(name = "AssetChangeDTO",value = "实物信息变更数据传输对象",required = true) AssetChangeDTO acDTO){

        System.out.println(acDTO);
        AssetChange assetChange = new AssetChange(0,acDTO.getChangeNo(),"未办理",acDTO.getChangeDate(),acDTO.getChangeHandler(),
                acDTO.getChangeBelongComp(),acDTO.getAssetIds(),acDTO.getAssetClassNew(),acDTO.getAssetModelNew(),acDTO.getAssetName(),
                acDTO.getAssetRegionNew(),acDTO.getAssetBuyDateNew(),acDTO.getAssetLifeNew(),acDTO.getAssetSourceNew(),acDTO.getAssetSupplierNew(),
                acDTO.getAssetUserCompNew(),acDTO.getAssetUserNew(),acDTO.getAssetPlace(),acDTO.getAssetDept(),acDTO.getRemarks(),null);

        return acService.saveAssetChange(assetChange);
    }

/*
*
 * @description:查询实物信息变更列表
 * @author: jianjiawei
 * @date: 2023/2/17 18:08
 **/
    @ApiOperation(value = "查询实物信息变更列表",notes = "显示实物信息变更列表信息",httpMethod = "get", response = AssetChangeVO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页显示总数",paramType = "query",dataType = "int",required = true)
    })
    @ResponseBody
    @RequestMapping(value = "/showAssetsChangeList",method = RequestMethod.GET)
    public R showAssetsChangeList(@RequestParam(name = "currentPage") Integer currentPage,@RequestParam(name = "pageSize") Integer pageSize){

        return acService.showAssetsChangeList(currentPage, pageSize);
    }


    /*
    *
     * @description:修改实物信息变更的信息回显
     * @author: jianjiawei 
     * @date: 2023/2/17 18:10
     **/
    @ApiOperation(value = "修改实物表单数据回显接口",notes = "修改实物信息变更的信息回显",httpMethod = "get", response = AssetChangeUpdateVO.class)
    @ApiImplicitParam(name = "id",value = "主键ID",paramType = "query",dataType = "int",required = true)
    @RequestMapping(value = "/showUpdateAssetsChange",method = RequestMethod.GET)
    @ResponseBody
    public R showUpdateAssetsChange(@RequestParam(name = "id") Integer id){

        return acService.showUpdateAssetsChange(id);
    }

    /*
    *
     * @description:实物信息变更修改的接口
     * @author: jianjiawei
     * @date: 2023/2/18 9:21
     **/
    @ApiOperation(value = "实物信息变更修改的接口",notes = "修改实物信息变更清单信息",httpMethod = "post", response = R.class)
    @ResponseBody
    @RequestMapping(value = "/updateAssetChangeSave",method = RequestMethod.POST)
    public R updateAssetChangeSave(@RequestBody @ApiParam(name = "AssetChangeDTO",value = "实物信息变更数据传输对象",required = true) AssetChangeDTO acDTO){

        return acService.updateAssetChangeSave(acDTO);

    }


    @ApiOperation(value = "删除实物信息变更的数据",notes = "删除实物信息数据",httpMethod = "post", response = R.class)
    @ApiImplicitParam(name = "id",value = "主键ID",paramType = "query",dataType = "int",required = true)
    @RequestMapping(value = "/deleteAssetsChange",method = RequestMethod.POST)
    @ResponseBody
    public R deleteAssetsChange(@RequestParam(name = "id") Integer id){

        return acService.deleteAssetsChange(id);
    }

    @ApiOperation(value = "高级搜索回显",notes = "回显高级搜索表单下拉框数据",httpMethod = "post", response = R.class)
    @RequestMapping(value = "/showAcAdvSearch",method = RequestMethod.GET)
    @ResponseBody
    public R showAcAdvSearch(){
        return acService.showAcAdvSearch();
    }


    @ApiOperation(value = "根据高级搜索查询实物信息变更接口",notes = "根据高级搜索查询所有实物信息变更列表",httpMethod = "post", response = AssetChangeVO.class)
    @ResponseBody
    @RequestMapping(value = "/findAssetsChangeListByAdv",method = RequestMethod.POST)
    public R findAssetsChangeListByAdv(@RequestBody @ApiParam(name = "AssetChangeAdvDTO",value = "实物信息变更高级搜索条件",required = true) AssetChangeAdvDTO advDTO){

        System.out.println(advDTO);
        return acService.findAssetsChangeListByAdv(advDTO);
    }


    @RequestMapping(value = "/exportExcel",method = RequestMethod.GET)
    @ApiOperation(value = "EXCEL导出",notes = "把实物变更信息导出为excel",httpMethod = "get")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=401,message ="用户未授权"),
            @ApiResponse(code=403,message="认证失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HSSFWorkbook workbook = acService.getWorkbook();
        String userAgent = request.getHeader("User-Agent");

        //excel文件名
        String fileName = "实物信息变更表"+System.currentTimeMillis()+".xls";
        // 针对IE或者以IE为内核的浏览器：
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
        } else {
            // 非IE浏览器的处理：
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        }
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            response.setContentType("application/x-xls;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            workbook.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
            try {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
    }

}
